#include <stdio.h>
#include <stdlib.h>

struct linkedlist_t
{
    struct linkedlist_t *previous;
    struct linkedlist_t *next;
    int num;
};

int solve(const struct linkedlist_t *base, int n);
struct linkedlist_t *linkedlist_cp(const struct linkedlist_t *source, int n);

int main(int argc, char *argv[])
{
    int n;
    scanf("%d", &n);

    struct linkedlist_t *base = NULL;
    base = (struct linkedlist_t *)malloc(sizeof(struct linkedlist_t) * n);
    base->previous = base + n - 1;
    (base + n - 1)->next = base;

    for (int i = 0; i < n - 1; i++)
    {
        scanf("%d", &base[i].num);
        base[i].next = base + i + 1;
        base[i + 1].previous = base + i;
    }
    scanf("%d", &base[n - 1].num);

    printf("%d\n", solve(base, n));
    free(base);
    return 0;
}

int solve(const struct linkedlist_t *base, int n)
{
    const struct linkedlist_t *ptr = base;
    printf("%s", "solve: ");
    for (int i = 0; i < n; i++)
    {
        printf("%d ", ptr->num);
        ptr = ptr->next;
    }
    printf("\n");

    int maxpower = 0;
    int power = 0;

    if (n == 2)
    {
        return (base->num > base->next->num) ? base->num * base->num * base->next->num
                                             : base->next->num * base->next->num * base->num;
    }

    struct linkedlist_t *ring = linkedlist_cp(base, n);
    struct linkedlist_t *destroyed_node;
    for (int i = 0; i < n; i++)
    {
        power = 0;
        destroyed_node = ring + i;
        destroyed_node->next->previous = destroyed_node->previous;
        destroyed_node->previous->next = destroyed_node->next;
        power += destroyed_node->num * destroyed_node->next->num * destroyed_node->previous->num;
        power += solve(destroyed_node->next, n - 1);
        if (maxpower < power)
            maxpower = power;
        destroyed_node->next->previous = destroyed_node;
        destroyed_node->previous->next = destroyed_node;
    }

    free(ring);
    return maxpower;
}

struct linkedlist_t *linkedlist_cp(const struct linkedlist_t *source, int n)
{
    struct linkedlist_t *dest = (struct linkedlist_t *)malloc(sizeof(struct linkedlist_t) * n);
    dest->previous = dest + n - 1;
    dest->previous->next = dest;
    for (int i = 0; i < n - 1; i++)
    {
        dest->next = dest + 1;
        dest->next->previous = dest;
        dest->num = source->num;
        dest++;
        source = source->next;
    }
    dest->num = source->num;
    dest = dest->next;

    return dest;
}
